/*
 *  Module dependencies
 */

var Ni = require('ni'),
    Mu = require('mu'),
    Quip = require('quip');

/*
 *  The calculator controller
 */

 
 
var GridexController = function() {
    
    /*
     *  This function is called when the URL does not indicate a function to
     *  be called, so it would look like /calculator.
     */

    this.index = function(req, res, next, a , b) {
            var data = {result: ''};
		Ni.showView(Ni.config('view_dir') + '/login.html',data, res);
		
		var game =  new Object;
		game.matrix = new Array (8);
		for (i = 0; i < game.matrix.length; ++ i){
			game.matrix[i] = new Array (8);
		}
		
		game.players = new Array(2);
			
		var player1 = new Object;
		player1.id = 1;
		player1.score = 0;
		player1.clicks = 0;
		game.players[0] = player1;
		var player2 = new Object;
		player2.id = 2;
		player2.score = 0;
		player2.clicks = 0;
		game.players[1] = player2;
			
		Ni.config('game',game);
		
		morepoints = function(){
			var maxStock = 5;
			game = Ni.config('game');
			game.players[0].clicks = (game.players[0].clicks +1);
			game.players[1].clicks = (game.players[1].clicks +1);
			if(game.players[0].clicks > maxStock)
				game.players[0].clicks = maxStock;
			if(game.players[1].clicks > maxStock)
				game.players[1].clicks = maxStock;
			Ni.config('game',game);
		}
		setInterval (morepoints, 800 );
	
    }

    /*
     *  This function is called when the URL indicates "add" as the function
     *  to be called, so it would look like /calculator/add.
     */
    
    this.play = function(req, res, next, pid) {
		
		game = Ni.config('game');
		if((pid == '1' || pid == '2') && game){
			
		
			var data = {pid: pid};
			Ni.showView(Ni.config('view_dir') + '/gridex.html',data, res);
		}
		else {
			res.error('cuek');
		}
    }
	
    this.move = function(req, res, next, x, y, pid) {
		var game = Ni.config('game');
		if(game){
			
			if(game.players[pid-1].clicks>0){
				game.players[pid-1].clicks = game.players[pid-1].clicks -1;
				
				if(game.matrix[x][y] == null){
					game.matrix[x][y] = pid
					//game.players[pid-1].score = game.players[pid-1].score + 1;
				}
				else if(game.matrix[x][y] !=  pid){
					game.matrix[x][y] = null;
					//game.players[pid-1].score = game.players[pid-1].score + 2;
				}
				
				Ni.config('game', game);
			}
		}
		var data = {pid: pid};
		res.write(JSON.stringify(data));
		res.end();
    }
	
    this.base = function(req, res, next, pid) {
		var game = Ni.config('game');
		if(game){
			
			todestroy = new Array (8);
			for (i = 0; i < todestroy.length; ++ i){
				todestroy[i] = new Array (8);
			}
			
			lines = 0;
			
			//chekeo vertical
			for (i=0; i<8; i++){
				var isDestroyed = true;
			
				for (j=0; j<8; j++){
					if(!(i ==0 && j==0 || i ==7 && j==7)){
						if(game.matrix[i][j] != pid){
							isDestroyed = false;
						}
					}
				}
				if(isDestroyed){
					lines++;
					for (j=0; j<8; j++){
						todestroy[i][j] = true;
					}
				}
			}
			
			//chekeo horizontal
			for (i=0; i<8; i++){
				var isDestroyed = true;
			
				for (j=0; j<8; j++){
					if(!(i ==0 && j==0 || i ==7 && j==7)){
						if(game.matrix[j][i] != pid){
							isDestroyed = false;
						}
					}
				}
				if(isDestroyed){
					lines++;
					for (j=0; j<8; j++){
						todestroy[j][i] = true;
					}
				}
			}
			
			score = 0;
			//destruccion
			for (i=0; i<8; i++){
				for (j=0; j<8; j++){
					if(todestroy[i][j]){
						game.matrix[i][j] = null;
						score++;
					}
				}
			}
			
			game.players[pid-1].score = game.players[pid-1].score + lines*score;
			Ni.config('game', game);
		
			var data = {pid: pid};
			res.write(JSON.stringify(data));
			res.end();
		}
    }
	
	this.game = function(req, res, next, x, y, pid) {
		var game = Ni.config('game');
		if(game){
			var data = Ni.config('game');
			res.write(JSON.stringify(data));
			res.end();
		}
	}
	
    this.test = function(req, res, next, a, b) {
        var data = {result: 'asdsad'};
		res.write(JSON.stringify(data));
		res.end();
    }
	
};

/*
 *  Exports the calculator controller
 */

module.exports = new GridexController();
